﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading;

namespace AgvDemo
{
    public class AgvDispatchClass
    {

        private static object lockDispatch = new object();//防止下位机数据处理异常
        public static object lockLocation = new object();//防止下位机数据处理异常
        private static object lockCallDispatch = new object();//防止下位机数据处理异常

        public int flagNum = 1;

        public int Flag141 = 0;
        public int Flag145 = 0;


        public void AgvDispatch(PlcManagerClass pmc)
        {
            try
            {
                if (pmc.chargeState9.CurrentStation == 14)//&&jigou1Up.jigouControl
                {
                    if (pmc.chargeState9.AgvState == 0 && MainManagerClass.mtc.js.UpPlcError == 0)
                    {
                        if (MainManagerClass.mtc.DoorUpAgv == 0)
                        {
                            MainManagerClass.mtc.DoorUpAgv = pmc.chargeState9.AgvNum;
                        }
                        else if (MainManagerClass.mtc.DoorUpAgv == pmc.chargeState9.AgvNum)
                        {
                            if (MainManagerClass.mtc.js.UpPlcAllowIn == 1)//
                            {
                                //if (MainManagerClass.mtc.js.UpAgvLeave == 1)
                                //{
                                //    byte[] tempV = { 0, 0 };
                                //    MainManagerClass.mtc.MBmaster_Write(3, tempV);
                                //}
                                //else
                                //{
                                pmc.StartAgv();
                                //}
                            }
                        }
                    }
                }
                else if (pmc.chargeState9.CurrentStation == 15)
                {
                    if (pmc.chargeState9.AgvState == 0 && MainManagerClass.mtc.js.UpPlcError == 0)
                    {
                        if (MainManagerClass.mtc.DoorUpAgv == pmc.chargeState9.AgvNum)
                        {
                            if (MainManagerClass.mtc.js.Raster == 1)
                            {
                                //关闭光栅
                                byte[] tempV = { 0, 0 };
                                MainManagerClass.mtc.MBmaster_Write(6, tempV);
                            }
                            else
                            {
                                pmc.StartAgv();
                            }
                        }
                    }

                }
                else if (pmc.chargeState9.CurrentStation == 2)
                {
                    if (MainManagerClass.mtc.DoorUpAgv == pmc.chargeState9.AgvNum)
                    {
                        try
                        {
                            if (MainManagerClass.mtc.js.Raster == 0)
                            {
                                //开启光栅
                                byte[] tempV = { 0, 1 };
                                MainManagerClass.mtc.MBmaster_Write(6, tempV);
                            }
                        }
                        catch
                        { }


                        if (pmc.chargeState9.AgvState == 0 && MainManagerClass.mtc.js.UpPlcError == 0)
                        {



                            if (MainManagerClass.mtc.js.UpAgvArrive == 0)
                            {
                                byte[] tempV = { 0, 1 };
                                MainManagerClass.mtc.MBmaster_Write(1, tempV);
                            }
                            else
                            {
                                if (pmc.chargeState9.AgvError == 0)
                                {
                                    if (MainManagerClass.mtc.js.UpPlcLeave == 1)
                                    {
                                        pmc.StartAgv();
                                    }

                                }
                            }
                        }
                        else if (pmc.chargeState9.AgvState == 1 && MainManagerClass.mtc.js.UpPlcError == 0)
                        {
                            if (MainManagerClass.mtc.js.UpPlcLeave == 1)
                            {
                                if (MainManagerClass.mtc.js.UpAgvArrive == 1)
                                {
                                    byte[] tempV = { 0, 0 };
                                    MainManagerClass.mtc.MBmaster_Write(1, tempV);
                                }
                            }
                        }

                        if (pmc.chargeState9.AgvError == 0)
                        {
                            if (MainManagerClass.mtc.js.UpAgvError == 1)
                            {
                                byte[] tempV = { 0, 0 };
                                MainManagerClass.mtc.MBmaster_Write(4, tempV);
                            }
                        }
                        else
                        {
                            if (MainManagerClass.mtc.js.UpAgvError == 0)
                            {
                                byte[] tempV = { 0, 1 };
                                MainManagerClass.mtc.MBmaster_Write(4, tempV);
                            }
                        }

                    }

                }
                else if (pmc.chargeState9.CurrentStation == 206)
                {
                    if (MainManagerClass.mtc.DoorUpAgv == pmc.chargeState9.AgvNum)
                    {
                        try
                        {
                            if (MainManagerClass.mtc.js.Raster == 0)
                            {
                                //关闭光栅
                                byte[] tempV = { 0, 1 };
                                MainManagerClass.mtc.MBmaster_Write(6, tempV);
                            }
                        }
                        catch
                        { }


                        if (MainManagerClass.mtc.js.UpAgvArrive == 1)
                        {
                            byte[] tempV = { 0, 0 };
                            MainManagerClass.mtc.MBmaster_Write(1, tempV);
                        }

                        if (pmc.chargeState9.AgvError == 0)
                        {
                            if (MainManagerClass.mtc.js.UpAgvError == 1)
                            {
                                byte[] tempV = { 0, 0 };
                                MainManagerClass.mtc.MBmaster_Write(4, tempV);
                            }
                        }
                        else
                        {
                            if (MainManagerClass.mtc.js.UpAgvError == 0)
                            {
                                byte[] tempV = { 0, 1 };
                                MainManagerClass.mtc.MBmaster_Write(4, tempV);
                            }
                        }
                    }
                }

                else if (pmc.chargeState9.CurrentStation == 180)
                {
                    if (MainManagerClass.mtc.DoorUpAgv == pmc.chargeState9.AgvNum)
                    {
                        if (MainManagerClass.mtc.js.UpAgvLeave == 0)
                        {
                            byte[] tempV = { 0, 1 };
                            MainManagerClass.mtc.MBmaster_Write(3, tempV);
                        }
                    }
                }

                else if (pmc.chargeState9.CurrentStation == 16)//&&jigou1Up.jigouControl
                {
                    if (MainManagerClass.mtc.DoorUpAgv == pmc.chargeState9.AgvNum)
                    {

                        if (MainManagerClass.mtc.js.UpAgvLeave == 1)
                        {
                            byte[] tempV = { 0, 0 };
                            MainManagerClass.mtc.MBmaster_Write(3, tempV);
                        }
                        else
                        {
                            MainManagerClass.mtc.DoorUpLeaveControl = 1;

                        }
                    }

                    if (pmc.chargeState9.AgvState == 0 && MainManagerClass.mtc.js.DownPlcError == 0)
                    {
                        if (MainManagerClass.mtc.DoorDownAgv == 0)
                        {
                            MainManagerClass.mtc.DoorDownAgv = pmc.chargeState9.AgvNum;
                        }
                        else if (MainManagerClass.mtc.DoorDownAgv == pmc.chargeState9.AgvNum)
                        {
                            if (MainManagerClass.mtc.js.DownAgvRequest == 0)
                            {
                                //写入请求
                                byte[] tempV = { 0, 1 };
                                MainManagerClass.mtc.MBmaster_Write(14, tempV);
                            }
                            else
                            {
                                if (MainManagerClass.mtc.js.DownPlcAllowIn == 1)//&& MainManagerClass.jigou1Up.chargeState9.AgvLeave == false
                                {
                                    if (MainManagerClass.mtc.js.DownAgvLeave == 1)
                                    {
                                        byte[] tempV = { 0, 0 };
                                        MainManagerClass.mtc.MBmaster_Write(11, tempV);
                                    }
                                    else
                                    {
                                        pmc.StartAgv();

                                    }


                                }
                            }



                        }
                    }

                }
                else if (pmc.chargeState9.CurrentStation == 21)
                {
                    if (MainManagerClass.mtc.DoorUpAgv == pmc.chargeState9.AgvNum)
                    {

                        MainManagerClass.mtc.DoorUpLeaveControl = 1;

                        if (MainManagerClass.mtc.js.UpAgvLeave == 1)
                        {
                            byte[] tempV = { 0, 0 };
                            MainManagerClass.mtc.MBmaster_Write(3, tempV);
                        }

                    }

                    //车门下线排除卡
                    if (MainManagerClass.mtc.DoorDownAgv == pmc.chargeState9.AgvNum)
                    {
                        if (MainManagerClass.mtc.js.DownPlcError == 0)
                        {
                            if (MainManagerClass.mtc.js.DownAgvArrive == 0)
                            {
                                byte[] tempV = { 0, 1 };
                                MainManagerClass.mtc.MBmaster_Write(9, tempV);
                            }
                        }


                        if (MainManagerClass.mtc.js.DownAgvRequest == 1)
                        {
                            //写入请求
                            byte[] tempV = { 0, 0 };
                            MainManagerClass.mtc.MBmaster_Write(14, tempV);
                        }

                        //关闭请求

                        if (pmc.chargeState9.AgvError == 0)
                        {
                            if (MainManagerClass.mtc.js.DownAgvError == 1)
                            {
                                byte[] tempV = { 0, 0 };
                                MainManagerClass.mtc.MBmaster_Write(12, tempV);
                            }
                        }
                        else
                        {
                            if (MainManagerClass.mtc.js.DownAgvError == 0)
                            {
                                byte[] tempV = { 0, 1 };
                                MainManagerClass.mtc.MBmaster_Write(12, tempV);
                            }
                        }
                    }


                }
                else if (pmc.chargeState9.CurrentStation == 121 || pmc.chargeState9.CurrentStation == 131 || pmc.chargeState9.CurrentStation == 101 || pmc.chargeState9.CurrentStation == 111)
                {

                }
                else
                {
                    if (MainManagerClass.mtc.DoorUpAgv == pmc.chargeState9.AgvNum)
                    {
                        MainManagerClass.mtc.DoorUpLeaveControl = 1;
                    }


                    if (MainManagerClass.mtc.DoorDownAgv == pmc.chargeState9.AgvNum)
                    {
                        MainManagerClass.mtc.DoorDownLeaveControl = 1;
                    }
                }
            }
            catch
            { }
        }




        public void BoxDispatch(PlcManagerClass pmc)
        {
            try
            {
                if (pmc.chargeState9.CurrentStation == 19)
                {
                    if (pmc.chargeState9.AgvState == 0)
                    {
                        if (MainManagerClass.boxAgv.AllowState == "03")
                        {
                            if (!pmc.chargeState9.BoxGo)
                                pmc.chargeState9.BoxGo = true;
                            else
                            {
                                MainManagerClass.boxAgv.SendMie();
                            }
                        }
                        else if (MainManagerClass.boxAgv.AllowState == "02")
                        {
                            if (pmc.chargeState9.BoxGo)
                            {
                                pmc.StartAgv();
                            }
                        }

                    }
                }
                else
                {
                    pmc.chargeState9.BoxGo = false;
                }
            }
            catch
            { }
        }
    }
}
